home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 2 / Apprentice-Release2.iso / Source Code / C / Applications / Eudora 1.3.1 / source / mywindow.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  24.7 KB  |  1,055 lines  |  [TEXT/MPS ]

  1. #define FILE_NUM 28
  2. /* Copyright (c) 1990-1992 by the University of Illinois Board of Trustees */
  3. #pragma load EUDORA_LOAD
  4. #pragma segment MyWindow
  5. #define W_INSET 1
  6. #define W_MIN 72
  7. void DrawMyGrow(MyWindowPtr);
  8. void HideMyControl(ControlHandle cntrlH);
  9. void ShowMyControl(ControlHandle cntrlH);
  10.  
  11. /**********************************************************************
  12.  * GetNewMyWindow - create a new window from a template
  13.  **********************************************************************/
  14. MyWindowPtr GetNewMyWindow(short resId,UPtr wStorage,WindowPtr behind,Boolean hBar, Boolean vBar)
  15. {
  16.     MyWindowPtr win;
  17.     Rect oldContR;
  18.     
  19.     if (wStorage == nil)
  20.     {
  21.         if (HandyWindow)
  22.         {
  23.             wStorage = HandyWindow;
  24.             HandyWindow = nil;
  25.         }
  26.         else if ((wStorage=NewPtr(sizeof(MyWindow)))==nil)
  27.             return (nil);
  28.     }
  29.     WriteZero(wStorage, sizeof(MyWindow));
  30.         
  31.     win = GetNewWindow(resId, wStorage, behind);
  32.     if (win==nil) return(nil);
  33.     
  34.     SetPort(win);
  35.     win->isDialog = False;
  36.     UsingWindow(win);
  37.         
  38.     /*
  39.      * add scroll bar(s)
  40.      */
  41.     if (hBar)
  42.     {
  43.         if (!(win->hBar = GetNewControl(SCROLL_CNTL,win)))
  44.             WarnUser(NO_CONTROL,ResError());
  45.         else
  46.         {
  47.             SetCtlAction(win->hBar,ScrollAction);
  48.             SetCtlMin(win->hBar,0);
  49.             SetCtlValue(win->hBar,0);
  50.             SetCtlMax(win->hBar,0);
  51.         }
  52.     }
  53.  
  54.     if (vBar)
  55.     {
  56.         if (!(win->vBar = GetNewControl(SCROLL_CNTL,win)))
  57.             WarnUser(NO_CONTROL,ResError());
  58.         else
  59.         {
  60.             SetCtlAction(win->vBar,ScrollAction);
  61.             SetCtlMin(win->vBar,0);
  62.             SetCtlValue(win->vBar,0);
  63.             SetCtlMax(win->vBar,0);
  64.         }
  65.     }
  66.  
  67.     win->hPitch = FontWidth;
  68.     win->vPitch = FontLead;
  69.     win->minSize.h = win->minSize.v = W_MIN;
  70.     TextFont(FontID);
  71.     TextSize(FontSize);
  72.     
  73.     /*
  74.      * do size-related computations
  75.      */
  76.     WriteZero(&oldContR,sizeof(Rect));
  77.     MyWindowDidResize(win,&oldContR);
  78.     
  79.     /*
  80.      * hooray!
  81.      */
  82.     win->isActive = FALSE;        /* let the activation event handle this */
  83.     
  84.     return(win);
  85. }
  86.  
  87. /**********************************************************************
  88.  * UpdateMyWindow - handle an update event for a window
  89.  **********************************************************************/
  90. void UpdateMyWindow(MyWindowPtr win)
  91. {
  92.     GrafPtr oldPort;
  93.     int maxH, maxV;
  94.     Rect r;
  95.     ControlHandle cntl;
  96.     
  97.     /*
  98.      * set the port
  99.      */
  100.     GetPort(&oldPort);
  101.     SetPort(win);
  102.     
  103.     /*
  104.      * let the window manager move regions for us
  105.      */
  106.     BeginUpdate(win);
  107.     
  108.     /*
  109.      * erase the invalid region
  110.      */
  111.     SetRect(&r,-INFINITY,-INFINITY,INFINITY,INFINITY);
  112.     EraseRect(&r);
  113.     
  114.     /*
  115.      * redraw the contents
  116.      */
  117.     maxH = win->hMax; maxV = win->vMax;
  118.     if (win->update) (*win->update)(win);
  119.     else if (win->txe)
  120.         TEUpdate(&((WindowPtr)win)->portRect,win->txe);
  121.     else if (win->ste)
  122.         STEUpdate(win->ste);
  123.     
  124.     /*
  125.      * draw the controls, etc.
  126.      */
  127.     if (!win->isDialog)
  128.     {
  129.       DrawMyControls(win);
  130.         if (!win->isActive || InBG)
  131.           for (cntl=win->qWindow.controlList;cntl;cntl=(*cntl)->nextControl)
  132.               if ((*cntl)->contrlRfCon == 'SCRL') FRAME_RECT(&(*cntl)->contrlRect);
  133.     }
  134.     else
  135.     {
  136.         DrawDialog(win);
  137.         HiliteButtonOne(win);
  138.     }
  139.     if (!win->isRunt) DrawMyGrow(win);
  140.  
  141.     /*
  142.      * put stuff back
  143.      */
  144.     EndUpdate(win);
  145.     
  146.     /*
  147.      * the update routine may have necessitated a change in the scroll bars
  148.      */
  149.     if (win->isActive&&
  150.             (win->hBar||win->vBar)&&(maxH!=win->hMax||maxV!=win->vMax))
  151.         DrawMyControls(win);
  152.             
  153.     /*
  154.      * reset the port
  155.      */
  156.     SetPort(oldPort);
  157. }
  158.  
  159. /**********************************************************************
  160.  * move a window
  161.  **********************************************************************/
  162. void DragMyWindow(MyWindowPtr win,Point thePt)
  163. {
  164.     Rect bounds;
  165.     
  166.     /*
  167.      * calculate a bounding rectangle
  168.      */
  169.     bounds = qd.screenBits.bounds;
  170.     bounds.top += GetMBarHeight();
  171.     InsetRect(&bounds,W_INSET,W_INSET);
  172.     
  173.     /*
  174.      * do it
  175.      */
  176.     DragWindow(win,thePt,&bounds);
  177.     
  178.     win->saveSize = True;
  179.     SFWTC = True;
  180. }
  181.  
  182. /**********************************************************************
  183.  * grow a window
  184.  **********************************************************************/
  185. void GrowMyWindow(MyWindowPtr win,Point thePt)
  186. {
  187.     GrafPtr oldPort;
  188.     Rect bounds;
  189.     long size;
  190.     Point *newSize = (Point *)&size;
  191.     Rect oldContR;
  192.     
  193.     /*
  194.      * save port
  195.      */
  196.     GetPort(&oldPort);
  197.     SetPort(win);
  198.  
  199.     /*
  200.      * calculate a bounding rectangle
  201.      */
  202.     bounds.top=win->minSize.v;
  203.     bounds.left=win->minSize.h;
  204.     bounds.bottom = bounds.right = INFINITY;
  205.     
  206.     /*
  207.      * grow it
  208.      */
  209.     size = GrowWindow(win,thePt,&bounds);
  210.     if (size)
  211.     {
  212.         Rect r;
  213.         
  214.         /*
  215.          * invalidate grow region
  216.          */
  217.         BlockMove(&((WindowPtr)win)->portRect,&r,sizeof(Rect));
  218.         r.left = r.right - GROW_SIZE;
  219.         r.top = r.bottom - GROW_SIZE;
  220.         InvalRect(&r);
  221.         
  222.         /*
  223.          * resize the window, for real
  224.          */
  225.         oldContR = win->contR;
  226.         SizeWindow(win,newSize->h,newSize->v,TRUE);
  227.         
  228.         /*
  229.          * diddle it
  230.          */
  231.         MyWindowDidResize(win,&oldContR);
  232.     
  233.         /*
  234.          * update it
  235.          */
  236.         UpdateMyWindow(win);
  237.         
  238.         win->saveSize = True;
  239.     }
  240.     
  241.     /*
  242.      * restore port
  243.      */
  244.     SetPort(oldPort);
  245.     SFWTC = True;
  246. }
  247.  
  248. /**********************************************************************
  249.  * zoom a window
  250.  **********************************************************************/
  251. void ZoomMyWindow(MyWindowPtr win,Point thePt,int partCode)
  252. {
  253.     GrafPtr oldPort;
  254.     Rect oldStd = StdState(win);
  255.     Rect oldUsr = UserState(win);
  256.     Rect curState = CurState(win);
  257.     Rect newStd;
  258.     Boolean wasZoomed = AboutSameRect(&curState,&oldStd);
  259.     
  260.     /*
  261.      * save port
  262.      */
  263.     GetPort(&oldPort);
  264.     SetPort(win);
  265.     
  266.     /*
  267.      * watch that mouse...
  268.      */
  269.     if (thePt.h == -1 || TrackBox(win,thePt,partCode))
  270.     {
  271.         Rect r;
  272.         
  273.         /*
  274.          * invalidate grow region
  275.          */
  276.         BlockMove(&((WindowPtr)win)->portRect,&r,sizeof(Rect));
  277.         r.left = r.right - GROW_SIZE;
  278.         r.top = r.bottom - GROW_SIZE;
  279.         InvalRect(&r);
  280.         
  281.         /*
  282.          * erase the window (grumble)
  283.          */
  284.         EraseRect(&r);
  285.         
  286.         /*
  287.          * figure new zoom rect
  288.          */
  289.         FigureZoom(win);
  290.         newStd = StdState(win);
  291.         
  292.         /*
  293.          * if we were not zoomed, save our current state as the user state
  294.          */
  295.         if (!wasZoomed) UserState(win) = curState;
  296.         
  297.         /*
  298.          * figure out what WE think the part code should be
  299.          */
  300.         partCode = (wasZoomed && AboutSameRect(&newStd,&oldStd)) ? inZoomIn : inZoomOut;
  301.  
  302.         /*
  303.          * zoom it
  304.          *
  305.          */
  306.         ZoomWindow(win,partCode,FALSE);
  307.         
  308.         /*
  309.          * if the window was in the "zoomed" state before, preserve the old
  310.          * user state; ZoomWindow will have set it to the previous state, which
  311.          * was the old standard state, which is not a very useful thing to do.
  312.          */
  313.         if (wasZoomed)
  314.             UserState(win) = oldUsr;
  315.         
  316.         /*
  317.          * note size change
  318.          */
  319.         MyWindowDidResize(win,nil);
  320.         
  321.         /*
  322.          * update it all (grumble, grumble)
  323.          */
  324.         InvalRect(&((WindowPtr)win)->portRect);
  325.  
  326.         /*
  327.          * update it
  328.          */
  329.         UpdateMyWindow(win);
  330.     }
  331.     
  332.     /*
  333.      * restore
  334.      */
  335.     SetPort(oldPort);
  336.     SFWTC = True;
  337. }
  338.  
  339. /**********************************************************************
  340.  * OffsetWindow - bump a window to its proper place
  341.  **********************************************************************/
  342. void OffsetWindow(MyWindowPtr win)
  343. {
  344.     short offset;
  345.     Point corner;
  346.     Rect oldContR;
  347.     
  348.     SetPort(win);
  349.     offset = GetMBarHeight();
  350.     if (IsMyWindow(win))
  351.     {
  352.         if (win->position)
  353.         {
  354.             oldContR = win->contR; 
  355.             if ((*win->position)(False,win))
  356.             {
  357.                 MyWindowDidResize(win,nil);
  358.                 return;
  359.             }
  360.         }
  361.         if (PrefIsSet(PREF_ZOOM_OPEN) && !win->isRunt)
  362.         {
  363.             Point pt;
  364. #ifdef WHAT_THE_HECK
  365.             Rect r = UserState(win);
  366.             void *oldPos = win->position;
  367.             win->position = nil;
  368.             MoveWindow(win,INFINITY/2,INFINITY/2,False);
  369.             ShowWindow(win);
  370.             EmptyRgn(win->qWindow.updateRgn);
  371.             UpdateMyWindow(win);
  372.             HideWindow(win);
  373. #endif WHAT_THE_HECK
  374.             pt.h = pt.v = -1;
  375.             ZoomMyWindow(win,pt,0);
  376.             return;
  377. #ifdef WHAT_THE_HECK
  378.             UserState(win) = r;
  379.             win->position = oldPos;
  380. #endif WHAT_THE_HECK
  381.         }
  382.     }
  383.     utl_StaggerWindow(&((GrafPtr)win)->portRect,1,offset,&corner,
  384.                                         GetRLong(PREF_STRN+PREF_NW_DEV));
  385.     MoveWindow(win,corner.h,corner.v,False);
  386.     if (IsMyWindow(win)) MyWindowDidResize(win,nil);
  387.     SFWTC = True;
  388. }
  389.  
  390. /**********************************************************************
  391.  * MyWindowDidResize - handle a window whose size has changed
  392.  **********************************************************************/
  393. void MyWindowDidResize(MyWindowPtr win,Rect *oldContR)
  394. {
  395.     Rect r;
  396.     Rect oldCont;
  397.     SAVE_PORT;
  398.     SetPort(win);
  399.     
  400.     if (!oldContR) SetRect(&oldCont,0,0,0,0);
  401.     else oldCont = *oldContR;
  402.     
  403.     /*
  404.      * make a couple of copies of the portRect
  405.      */
  406.     r = ((GrafPtr)win)->portRect;
  407.     r.top += win->topMargin;
  408.     win->contR = r;
  409.     
  410.     /*
  411.      * handle scroll bars
  412.      */
  413.     if (win->hBar)
  414.     {
  415.         MoveMyCntl(win,win->hBar,r.left-1,r.bottom-GROW_SIZE,r.right-GROW_SIZE+2-r.left,GROW_SIZE+1);
  416.         win->contR.bottom -= GROW_SIZE;
  417.     }
  418.     if (win->vBar)
  419.     {
  420.         MoveMyCntl(win,win->vBar,r.right-GROW_SIZE,r.top-1,GROW_SIZE+1,r.bottom-r.top-GROW_SIZE+2);
  421.         win->contR.right -= GROW_SIZE;
  422.     }
  423.                                     
  424.     /*
  425.      * grow region
  426.      */
  427.     r.left = r.right - GROW_SIZE;
  428.     r.top = r.bottom - GROW_SIZE;
  429.     InvalRect(&r);
  430.     
  431.     /*
  432.      * window-specific adjustments
  433.      */
  434.     if (win->didResize)
  435.         (*win->didResize)(win,&oldCont);
  436.     
  437.     /*
  438.      * now the scroll bars
  439.      */
  440.     MyWindowMaxes(win,win->hMax,win->vMax);
  441.     REST_PORT;
  442. }
  443.  
  444. /**********************************************************************
  445.  * MoveMyCntl - move a control attached to a window
  446.  **********************************************************************/
  447. void MoveMyCntl(MyWindowPtr win,ControlHandle cntl,int h,int v,int w,int t)
  448. {
  449. #pragma unused(win)
  450.     Rect r;
  451.     Boolean oldVis = (*cntl)->contrlVis;
  452.     
  453.     if (oldVis)
  454.     {
  455.         HideControl(cntl);
  456.         r=(*cntl)->contrlRect;
  457.         InsetRect(&r,-1,-1);
  458.         InvalRect(&r);
  459.     }
  460.     MoveControl(cntl,h,v);
  461.     SizeControl(cntl,w,t);
  462.     if (oldVis)
  463.     {
  464.         (*cntl)->contrlVis = True;
  465.         r=(*cntl)->contrlRect;
  466.         InsetRect(&r,-1,-1);
  467.         InvalRect(&r);
  468.     }
  469. }
  470.  
  471. /**********************************************************************
  472.  * ScrollMyWindow - scroll a window
  473.  **********************************************************************/
  474. void ScrollMyWindow(MyWindowPtr win,int h,int v)
  475. {
  476.     RgnHandle updateRgn;
  477.     GrafPtr oldPort;
  478.     
  479.     GetPort(&oldPort);
  480.     SetPort(win);
  481.     
  482.     if (!h && !v) return;
  483.     
  484.     if (win->ste)
  485.       STEScroll(win->ste,h,v);
  486.     else if (!win->scroll || (*win->scroll)(win,h,v))
  487.     {
  488.         /*
  489.          * create a region for ScrollRect to use
  490.          */
  491.         updateRgn = NewRgn();
  492.         if (win->isActive && win->txe) INVAL_RECT(&(*win->txe)->selRect);
  493.         
  494.         ScrollRect(&win->contR,h*win->hPitch,v*win->vPitch,updateRgn);
  495.         
  496.         /*
  497.          * update region maintenance
  498.          */
  499.         InvalRgn(updateRgn);
  500.         DisposeRgn(updateRgn);
  501.     }
  502.     SetPort(oldPort);
  503. }
  504.  
  505. /**********************************************************************
  506.  * MyWindowMaxes    - set the max values for the scroll bars
  507.  * takes the maximum number of units in each direction, and subtracts
  508.  * the number of units represented by the window.
  509.  **********************************************************************/
  510. void MyWindowMaxes(MyWindowPtr win,int hMax,int vMax)
  511. {
  512.     int h=0,v=0;
  513.     
  514.     if (win->hBar)
  515.     {
  516.         win->hMax = hMax;
  517.         h = BarMax(win->hBar,hMax,win->contR.right-win->contR.left,win->hPitch);
  518.     }
  519.     if (win->vBar)
  520.     {
  521.         win->vMax = vMax;
  522.         v = BarMax(win->vBar,vMax,win->contR.bottom-win->contR.top,win->vPitch);
  523.     }
  524.     ScrollMyWindow(win,h,v);
  525. }
  526.  
  527. /**********************************************************************
  528.  * BarMax - set the max value for a scroll bar
  529.  **********************************************************************/
  530. int BarMax(ControlHandle cntl,int max,int winSize,int pitch)
  531. {
  532.         int old;
  533.         
  534.         old = GetCtlValue(cntl);
  535.         max -= winSize/pitch;
  536.         if (max < 0) max = 0;
  537.         SetCtlMax(cntl,max);
  538.         return(old>max ? old-max : 0);
  539. }
  540.  
  541. /**********************************************************************
  542.  * ScrollAction - action procedure for scroll bar
  543.  **********************************************************************/
  544. pascal void ScrollAction(ControlHandle theCtl,short partCode)
  545. {
  546.     MyWindowPtr win = (*theCtl)->contrlOwner;
  547.     short inc=0,page;
  548.     Boolean isH;
  549.     Rect r = (*theCtl)->contrlRect;
  550.     static uLong lastTicks;
  551.     
  552.     if (TickCount()-lastTicks<4) return;
  553.     else lastTicks = TickCount();
  554.   isH = r.right-r.left > r.bottom-r.top;
  555.     page = (isH ? RoundDiv(r.right-r.left,win->hPitch)
  556.                              : RoundDiv(r.bottom-r.top,win->vPitch))-1;
  557.  
  558.     switch(partCode)
  559.     {
  560.         case inUpButton:
  561.             inc = -1;
  562.             break;
  563.         case inDownButton:
  564.             inc = 1;
  565.             break;
  566.         case inPageUp:
  567.             inc = -page;
  568.             break;
  569.         case inPageDown:
  570.             inc = page;
  571.             break;
  572.     }
  573.     
  574.     if (inc) inc = -IncMyCntl(theCtl,inc);
  575.     if (inc)
  576.     {
  577.         if (isH)
  578.             ScrollMyWindow(win,inc,0);
  579.         else
  580.             ScrollMyWindow(win,0,inc);
  581.         UpdateMyWindow(win);
  582.     }
  583. }
  584.  
  585.  
  586. /************************************************************************
  587.  * WinTEH - grab the active terec for a window
  588.  ************************************************************************/
  589. TEHandle WinTEH(MyWindowPtr win)
  590. {
  591.     if (IsMyWindow(win))
  592.     {
  593.         if (win->txe) return(win->txe);
  594.         if (win->ste) return((*(STEHandle)win->ste)->te);
  595.     }
  596.     return(nil);
  597. }
  598.  
  599. /************************************************************************
  600.  * ScrollIsH - is a scroll bar a horizontal one?
  601.  ************************************************************************/
  602. Boolean ScrollIsH(ControlHandle cntl)
  603. {
  604.     Rect r = (*cntl)->contrlRect;
  605.  
  606.   return(r.right-r.left > r.bottom-r.top);
  607. }
  608.  
  609. /**********************************************************************
  610.  * ActivateMyWindow - handle an activate event for one of my windows
  611.  **********************************************************************/
  612. void ActivateMyWindow(MyWindowPtr win,Boolean active)
  613. {
  614.     GrafPtr oldPort;
  615.     ControlHandle cntl;
  616.     TEHandle teh = WinTEH(win);
  617.     
  618.     if (!win) return;
  619.     GetPort(&oldPort);
  620.     SetPort(win);
  621.     
  622.     win->isActive = active;
  623.     
  624.     for (cntl=win->qWindow.controlList;cntl;cntl=(*cntl)->nextControl)
  625.     {
  626.       if ((*cntl)->contrlRfCon == 'SCRL')
  627.         {
  628.             if (!active)
  629.                 HideMyControl(cntl);
  630.             else if (!InBG)
  631.                 ShowMyControl(cntl);
  632.         }
  633.     }
  634.             
  635.     if (!win->isRunt) DrawMyGrow(win);
  636.  
  637.     if (win->activate)
  638.         (*win->activate)(win);
  639.  
  640.     if (teh)
  641.     {
  642.         if (win->isActive)
  643.             TEActivate(teh);
  644.         else
  645.             TEDeactivate(teh);
  646.     }
  647.     
  648.     if (win->isDialog)
  649.     {
  650.         MenuHandle mh = GetMHandle(EDIT_MENU);
  651.         if (mh)
  652.         {
  653.             EnableIf(mh,EDIT_COPY_ITEM,active);
  654.             EnableIf(mh,EDIT_CUT_ITEM,active);
  655.             EnableIf(mh,EDIT_PASTE_ITEM,active);
  656.             EnableIf(mh,EDIT_CLEAR_ITEM,active);
  657.             EnableIf(mh,EDIT_UNDO_ITEM,active);
  658.         }
  659.         HiliteButtonOne(win);
  660.     }
  661.     
  662.     if (active) SFWTC=True;
  663.     
  664.     SetPort(oldPort);
  665. }
  666.  
  667. /**********************************************************************
  668.  * InvertLine - invert a line in one of my windows.  assumes origin
  669.  * is set correctly.
  670.  **********************************************************************/
  671. void InvertLine(MyWindowPtr win,int line)
  672. {
  673.     Rect r;
  674.     
  675.     r.top = (line-GetCtlValue(win->vBar))*win->vPitch;
  676.     r.bottom = r.top + win->vPitch;
  677.     r.left = 0;
  678.     r.right = INFINITY;
  679.     SectRect(&r,&win->contR,&r);
  680.     BitClr((Ptr)HiliteMode, pHiliteBit);
  681.     InvertRect(&r);
  682. }
  683.  
  684. /**********************************************************************
  685.  * CloseMyWindow - close a window, asking for confirmation if necessary
  686.  **********************************************************************/
  687. Boolean CloseMyWindow(MyWindowPtr win)
  688. {
  689.     Boolean rmHelp = HasHelp && win==(MyWindowPtr)FrontWindow();
  690.  
  691.     if (win->saveSize && win->position) (*win->position)(True,win);
  692.     if (win->close && !(*win->close)(win)) return(False);
  693.     if (win->txe) TEDispose(win->txe);
  694.     NukeUndo(win);
  695.     
  696.     win->isDialog ? DisposDialog(win) : DisposeWindow(win);
  697.     SFWTC = True;
  698.     if (rmHelp) HMRemoveBalloon();
  699.  
  700.     return(True);
  701. }
  702.  
  703. /**********************************************************************
  704.  * GoAwayMyWindow - track a click in the go away box
  705.  **********************************************************************/
  706. void GoAwayMyWindow(WindowPtr win,Point pt)
  707. {
  708.     SetPort(win);
  709.     if (TrackGoAway(win,pt))
  710.         CloseMyWindow(win);
  711. }
  712.  
  713. /**********************************************************************
  714.  * DrawMyGrow - draw a grow icon, taking into account what scroll bars
  715.  * the window has
  716.  **********************************************************************/
  717. #define BIGSIDE 8
  718. #define SMALLSIDE 6
  719. #define OFFSET 4
  720. void DrawMyGrow(MyWindowPtr win)
  721. {
  722.     Rect r;
  723.     
  724.     /*
  725.      * draw the borders of the scroll bars (if any, and if the window
  726.      * is inactive (the scroll bars will be drawn elsewhere if the
  727.      * window is active)
  728.      */
  729.     if (!win->isActive)
  730.     {
  731.         if (win->hBar)
  732.         {
  733.             MoveTo(win->contR.left,win->contR.bottom);
  734.             LineTo(INFINITY,win->contR.bottom);
  735.         }
  736.         if (win->vBar)
  737.         {
  738.             MoveTo(win->contR.right,win->contR.top);
  739.             LineTo(win->contR.right,INFINITY);
  740.         }
  741.     }
  742.  
  743.     r = ((GrafPtr)win)->portRect;
  744.     r.left = r.right - GROW_SIZE;
  745.     r.top = r.bottom - GROW_SIZE;
  746.     {
  747.         RgnHandle oldClip = ((GrafPtr)win)->clipRgn;
  748.         ((GrafPtr)win)->clipRgn=NewRgn();
  749.         ClipRect(&r);
  750.         DrawGrowIcon(win);
  751.         DisposeRgn(((GrafPtr)win)->clipRgn);
  752.         ((GrafPtr)win)->clipRgn = oldClip;
  753.     }
  754. }
  755.  
  756. /**********************************************************************
  757.  * HideMyControl - hide a control, but do it right
  758.  **********************************************************************/
  759. void HideMyControl(ControlHandle cntrlH)
  760. {
  761.     (*cntrlH)->contrlVis = 0;
  762.     WhiteRect(&(*cntrlH)->contrlRect);
  763.     VALID_RECT(&(*cntrlH)->contrlRect);
  764. }
  765.  
  766. /**********************************************************************
  767.  * ShowMyControl - show a control, but do it right
  768.  **********************************************************************/
  769. void ShowMyControl(ControlHandle cntrlH)
  770. {
  771.     ShowControl(cntrlH);
  772.     VALID_RECT(&(*cntrlH)->contrlRect);
  773. }
  774.  
  775. /************************************************************************
  776.  * ShowMyWindow - make a window visible
  777.  ************************************************************************/
  778. void ShowMyWindow(MyWindowPtr win)
  779. {
  780.     short kind = win->qWindow.windowKind;
  781.     
  782.     if (win->qWindow.visible) return;    /* do nothing */
  783.     if (win->gonnaShow) (*win->gonnaShow)(win);
  784.     OffsetWindow(win);
  785.     ShowWindow(win);
  786.     SelectWindow(win);
  787.     ActivateMyWindow(win,!InBG);
  788.     SetRectRgn(win->qWindow.updateRgn,-INFINITY/2,-INFINITY/2,INFINITY/2,INFINITY/2);
  789.     SFWTC = True;
  790. }
  791.  
  792. /************************************************************************
  793.  * MyClikLoop - autoscroll a TERec
  794.  ************************************************************************/
  795. pascal Boolean MyClikLoop(void)
  796. {
  797.     MyWindowPtr win=FrontWindow();
  798.     static long lastTicks;
  799.     Rect r;
  800.     
  801.     if (TickCount()-lastTicks>3 && ((WindowPeek)win)->windowKind >= userKind)
  802.     {
  803.         Point pt;
  804.         int deltaV=0;
  805.         
  806.         if (ClickType==Triple) TESelPara(win->txe);
  807.         lastTicks = TickCount();
  808.         GetMouse(&pt);
  809.         if (pt.v < win->contR.top)
  810.             deltaV = (win->contR.top-pt.v)*3/(int)win->vPitch/4+1;
  811.         else if (pt.v > win->contR.bottom)
  812.             deltaV = (win->contR.bottom-pt.v)*3/(int)win->vPitch/4-1;
  813.         if (deltaV)
  814.         {
  815.             if (((WindowPeek)win)->windowKind==COMP_WIN)
  816.             {
  817.                 r = win->contR;
  818.                 if (deltaV>0)
  819.                     r.bottom = r.top + (deltaV+2)*win->vPitch;
  820.                 else
  821.                     r.top = r.bottom - (2-deltaV)*win->vPitch;
  822.                 InvalRect(&r);
  823.             }
  824.             ScrollIt(win,0,deltaV);
  825.             if (((WindowPeek)win)->windowKind==COMP_WIN)
  826.             {
  827.                 SetRectRgn(((GrafPtr)win)->clipRgn,(*win->txe)->viewRect.left,
  828.                                 (*win->txe)->viewRect.top,(*win->txe)->viewRect.right,
  829.                                 (*win->txe)->viewRect.bottom);
  830.             }
  831.         }
  832.     }
  833.     return(True);
  834. }
  835.  
  836. /************************************************************************
  837.  * ContentClip - set a windows's clipping region to its content region
  838.  ************************************************************************/
  839. void ContentClip(MyWindowPtr win)
  840. {
  841.     SetPort(win);
  842.     ClipRect(&win->contR);
  843. }
  844.  
  845. /************************************************************************
  846.  * InfiniteClip - set a window's clipping region to infinity
  847.  ************************************************************************/
  848. void InfiniteClip(MyWindowPtr win)
  849. {
  850.     Rect r;
  851.     SetPort(win);
  852.     SetRect(&r,-INFINITY,-INFINITY,INFINITY,INFINITY);
  853.     ClipRect(&r);
  854. }
  855.  
  856. /************************************************************************
  857.  * ScrollIt - scroll a window, doing all the nasty things that have to
  858.  * be done.
  859.  ************************************************************************/
  860. void ScrollIt(MyWindowPtr win,int deltaH,int deltaV)
  861. {
  862.     Handle oldClip;
  863.     int hMin,hMax,hVal;
  864.     int vMin,vMax,vVal;
  865.     
  866.     SetPort(win);
  867.     if (deltaH && win->hBar)
  868.     {
  869.         hMin = GetCtlMin(win->hBar);
  870.         hMax = GetCtlMax(win->hBar);
  871.         hVal = GetCtlValue(win->hBar);
  872.         if (deltaH<0)
  873.         {
  874.             if (deltaH<hVal-hMax) deltaH = hVal-hMax;
  875.         }
  876.         else
  877.         {
  878.             if (deltaH>hVal-hMin) deltaH = hVal-hMin;
  879.         }
  880.     }
  881.     else deltaH = 0;
  882.     if (deltaV && win->vBar)
  883.     {
  884.         vMin = GetCtlMin(win->vBar);
  885.         vMax = GetCtlMax(win->vBar);
  886.         vVal = GetCtlValue(win->vBar);
  887.         if (deltaV<0)
  888.         {
  889.             if (deltaV<vVal-vMax) deltaV = vVal-vMax;
  890.         }
  891.         else
  892.         {
  893.             if (deltaV>vVal-vMin) deltaV = vVal-vMin;
  894.         }
  895.     }
  896.     else deltaV = 0;
  897.     
  898.     if (deltaH || deltaV)
  899.     {     
  900.         oldClip = win->qWindow.port.clipRgn;
  901.         win->qWindow.port.clipRgn = NewRgn();
  902.         InfiniteClip(win);
  903.         if (deltaH) SetCtlValue(win->hBar,hVal-deltaH);
  904.         if (deltaV) SetCtlValue(win->vBar,vVal-deltaV);
  905.         ScrollMyWindow(win,deltaH,deltaV);
  906.         UpdateMyWindow(win);
  907.         DisposeRgn(win->qWindow.port.clipRgn);
  908.         win->qWindow.port.clipRgn = oldClip;
  909.     }
  910. }
  911.  
  912. /************************************************************************
  913.  * EraseUpdateRgn - erase a window's update region
  914.  ************************************************************************/
  915. void EraseUpdateRgn(WindowPeek win)
  916. {
  917.     Point origin;
  918.     
  919.     SetPort(win);
  920.     origin.h = origin.v = 0;
  921.     LocalToGlobal(&origin);
  922.     OffsetRgn(win->updateRgn,-origin.h,-origin.v);
  923.     EraseRgn(win->updateRgn);
  924.     OffsetRgn(win->updateRgn,origin.h,origin.v);
  925. }
  926.  
  927. /************************************************************************
  928.  * InvalContent - invalidate a window's content region
  929.  ************************************************************************/
  930. void InvalContent(MyWindowPtr win)
  931. {
  932.     GrafPtr oldPort;
  933.     
  934.     GetPort(&oldPort);
  935.     SetPort(win);
  936.     InvalRect(&win->contR);
  937.     SetPort(oldPort);
  938. }
  939.  
  940. /************************************************************************
  941.  * CalcCntlInc - do the necessary drudge work before changing a control
  942.  ************************************************************************/
  943. short CalcCntlInc(ControlHandle cntl,short tentativeInc)
  944. {
  945.     short current,max,min,new;
  946.     
  947.     if (!tentativeInc) return(0);
  948.     new = current = GetCtlValue(cntl);
  949.     if (tentativeInc<0)
  950.     {
  951.         min = GetCtlMin(cntl);
  952.         if (current > min) new = MAX(min,current+tentativeInc);
  953.     }
  954.     else
  955.     {
  956.         max = GetCtlMax(cntl);
  957.         if (current < max) new = MIN(max,current+tentativeInc);
  958.     }
  959.     
  960.     return(new-current);
  961. }
  962.  
  963. /************************************************************************
  964.  * IncMyCntl - change a control.  Returns the change in value.
  965.  ************************************************************************/
  966. short IncMyCntl(ControlHandle cntl,short inc)
  967. {
  968.     inc = CalcCntlInc(cntl,inc);
  969.     if (inc) SetCtlValue(cntl,GetCtlValue(cntl)+inc);
  970.     return(inc);
  971. }
  972.  
  973. /************************************************************************
  974.  * UsingWindow - mark a window as in use
  975.  ************************************************************************/
  976. void UsingWindow(MyWindowPtr win)
  977. {
  978.     if (IsMyWindow(win)) win->inUse = True;
  979. }
  980.  
  981. /************************************************************************
  982.  * NotUsingWindow - mark a window as not in use
  983.  ************************************************************************/
  984. void NotUsingWindow(MyWindowPtr win)
  985. {
  986.     if (IsMyWindow(win)) win->inUse = False;
  987. }
  988.  
  989. /************************************************************************
  990.  * NotUsingAllWindows - mark all windows as not in use
  991.  ************************************************************************/
  992. void NotUsingAllWindows(void)
  993. {
  994.     MyWindowPtr win;
  995.     
  996.     for (win=FrontWindow();win;win=win->qWindow.nextWindow)
  997.         NotUsingWindow(win);
  998. }
  999.  
  1000. /************************************************************************
  1001.  * FigureZoom - figure out the proper zoomed state for a window
  1002.  ************************************************************************/
  1003. void FigureZoom(MyWindowPtr win)
  1004. {
  1005.     GDHandle gd;
  1006.     Rect zoom,regular;
  1007.     Boolean hasMB;
  1008.     short titleHi;
  1009.     Point corner;
  1010.     Boolean isVis = win->qWindow.visible;
  1011.     
  1012.     utl_GetWindGD(win,&gd,&zoom,®ular,&hasMB);
  1013.     InsetRect(&zoom,2,2);
  1014.     zoom.left += GetRLong(DESK_LEFT_STRIP);
  1015.     zoom.right -= GetRLong(DESK_RIGHT_STRIP);
  1016.     zoom.bottom -= GetRLong(DESK_BOTTOM_STRIP);
  1017.     zoom.top += GetRLong(DESK_TOP_STRIP);
  1018.     if (hasMB) zoom.top += GetMBarHeight();
  1019.     if (isVis)
  1020.         titleHi = (*win->qWindow.contRgn)->rgnBBox.top -
  1021.                             (*win->qWindow.strucRgn)->rgnBBox.top;
  1022.     else
  1023.         titleHi = titleBarHeight;
  1024.     zoom.top += titleHi;
  1025.     
  1026.     if (IsMyWindow(win) && win->zoomSize) (*win->zoomSize)(win,&zoom);
  1027.     corner.h = corner.v = 0;
  1028.     
  1029.     win->qWindow.visible = False;    /* make staggerwindow ignore us */
  1030.     utl_StaggerWindow(&zoom,1,GetMBarHeight(),&corner,GDIndexOf(gd));
  1031.     win->qWindow.visible = isVis;    /* put visible back */
  1032.     OffsetRect(&zoom,corner.h-zoom.left,corner.v-zoom.top);
  1033.     
  1034.     StdState(win) = zoom;
  1035. }
  1036.  
  1037. /************************************************************************
  1038.  * GDIndexOf - find the index of a given gd
  1039.  ************************************************************************/
  1040. short GDIndexOf(GDHandle gd)
  1041. {
  1042.     short i = 0;
  1043.     GDHandle candidate;
  1044.     Rect r;
  1045.     Boolean b;
  1046.     
  1047.     if (utl_HaveColor())
  1048.     {
  1049.       for (utl_GetIndGD(++i,&candidate,&r,&b);candidate;utl_GetIndGD(++i,&candidate,&r,&b))
  1050.           if (candidate==gd) return(i);
  1051.     }
  1052.     return(0);
  1053. }
  1054.  
  1055.